/*
 * Copyright 2010 Nicholas Alexander Cross
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.intellij.lang.plist.parser;

import com.intellij.lang.ASTNode;
import com.intellij.lang.LanguageUtil;
import com.intellij.lang.ParserDefinition;
import com.intellij.lang.PsiParser;
import com.intellij.lang.xml.XMLLanguage;
import com.intellij.lexer.Lexer;
import com.intellij.openapi.project.Project;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.IFileElementType;
import com.intellij.psi.tree.TokenSet;
import org.intellij.lang.plist.psi.impl.PListArrayImpl;
import org.intellij.lang.plist.psi.impl.PListDictionaryImpl;
import org.intellij.lang.plist.psi.impl.PListElementImpl;
import org.intellij.lang.plist.psi.impl.PListPsiFileImpl;
import org.jetbrains.annotations.NotNull;

public class PListParserDefinition implements ParserDefinition {

    @NotNull
    public Lexer createLexer(Project project) {
        return new PListLexer();
    }

    public PsiParser createParser(Project project) {
        return new PListParser();
    }

    public IFileElementType getFileNodeType() {
        return PListElementTypes.FILE;
    }

    @NotNull
    public TokenSet getWhitespaceTokens() {
        return PListElementTypes.WHITE_SPACE_TOKENS;
    }

    @NotNull
    public TokenSet getCommentTokens() {
        return PListElementTypes.COMMENT_TOKENS;
    }

    @NotNull
    public TokenSet getStringLiteralElements() {
        return PListElementTypes.STRING_TOKENS;
    }

    @NotNull
    public PsiElement createElement(ASTNode node) {
        IElementType type = node.getElementType();

        if (type == PListElementTypes.ARRAY) {
            return new PListArrayImpl(node);
        } else if (type == PListElementTypes.DICTIONARY) {
            return new PListDictionaryImpl(node);
        }

        return new PListElementImpl(node);
    }

    public PsiFile createFile(FileViewProvider viewProvider) {
        //hack: this hack will disable error detection for xml files, it doesn't work if added after the file is already loaded
        //i think it is better to just make it so the parser detects the <?xml version=\"1.0\" encoding=\"UTF-8\"?>
        //and goes into a different start where it doesn't match any tokens, perhaps returning an XMLToken there after.

        /*if (viewProvider.getContents().toString().contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) {
            return viewProvider.getPsi(XMLLanguage.INSTANCE);
        }*/

        return new PListPsiFileImpl(viewProvider);
    }

    public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) {
        final Lexer lexer = createLexer(left.getPsi().getProject());
        return LanguageUtil.canStickTokensTogetherByLexer(left, right, lexer);
    }

}
